﻿using System.Xml.Linq;

namespace Maple.Weixin.MP.Entities.Messages
{
    public sealed class ResponseMessageDkf : ResponseMessageBase
    {
        private readonly RequestMessageBase requestMessage;
        private readonly CustomerServiceAccount account;

        internal ResponseMessageDkf(RequestMessageBase requestMessage) {
            this.requestMessage = requestMessage;
        }

        internal ResponseMessageDkf(RequestMessageBase requestMessage, CustomerServiceAccount account)
            : this(requestMessage) {
            this.account = account;
        }

        internal ResponseMessageDkf(RequestMessageBase requestMessage, string kfAccount)
            : this(requestMessage, new CustomerServiceAccount {KfAccount = kfAccount}) {
            
        }

        public override ResponseMessageType MessageType {
            get { return ResponseMessageType.TransferCustomerService; }
        }

        public override XDocument ToXDocumentBase() {
            // 保留请求的xml，只是将MsgType改为transfer_customer_service
            var requestXml = requestMessage.XDocument.ToString();
            var responseDocument = XDocument.Parse(requestXml);
            var element = responseDocument.Element("MsgType");
            if (element != null)
            {
                element.ReplaceWith(new XCData("transfer_customer_service"));
                if (account != null) {
                    element.AddAfterSelf(new XElement("TransInfo",
                        new XElement("KfAccount", new XCData(account.KfAccount))));
                }
            }
            return responseDocument;
        }
    }

    /// <summary>
    /// 多客服账号。
    /// </summary>
    public class CustomerServiceAccount
    {
        public string KfAccount { get; set; }
    }
}
